import { createRouter, createWebHistory } from 'vue-router';  
import Home from '../src/views/Home.vue';  
import About from '../src/views/About.vue';  
import Login from '../src/views/Login.vue'  
import Register from '../src/views/Register.vue';
import Detail from '../src/views/Detail.vue';
import Submit from '../src/views/Submit.vue'
import Person from '../src/views/Person.vue'
import PersonEdit from '../src/views/PersonEdit.vue'
import Success from "../src/views/Success.vue"
import { useMainStore } from "../store";


const routes = [  
  { path: '/', name: 'Home', component: Home },  
  { path: '/about', name: 'About', component: About },  
  { path: '/login', name: 'Login', component: Login },
  { path: '/register', name: 'Register', component: Register },
  { path: '/detail/:id', name: 'Detail', component: Detail },
  { path: '/submit/:id', name: 'Submit', component: Submit },
  { path: '/person/:id', name: 'Person', component: Person },
  { path: '/person/edit/:id', name: 'PersonEdit', component: PersonEdit },
  { path: '/success', name: 'Success', component: Success },
  // 添加其他路由...  
];  
  
const router = createRouter({  
  history: createWebHistory(),  
  routes, // 路由配置  
});  

router.beforeEach((to, from, next) => {  
    const useStore = useMainStore();
    if(to.path == "/login" || to.path == "/register" || to.path == "/"){
      next();
    }else{
      if(useStore.token == ""){
        next("/login");
      }else{
        next();
      }
    }
    
    
  })
  
export default router;